/**
 * project mapper
 * @author wuji
 * @date 2020-07-19
 */
const {db} = require("../db/db.js");

/**
 * Select All Projects
 */
exports.selectAllProject = function(cb) {
    db.all("select id, name, cid as cId, orders from tb_project order by orders", cb);
}

/**
 * Select Project By Id
 * @param {*} projectId 
 */
exports.selectProjectById = function(id, cb) {
    db.all("select id, name, cid as cId, orders from tb_project where id = ?", id, cb);
}

/**
 * Add Project
 * @param {*} project 
 */
exports.addProject = function(project, cb) {
    db.run("insert into tb_project (name, cid, orders) values (?,?,?)",
        [project.name, project.cId, project.orders], cb);
}

/**
 * Delete Project By Id
 * @param {*} pId 
 */
exports.deleteProjectById = function(id, cb) {
    db.run("delete from tb_project where id = ?", id, cb);
}

/**
 * Delete All Projects Of The Company
 * @param {*} cId 
 */
exports.deleteProjectsByCompanyId = function(cId, cb) {
    db.run("delete from tb_project where cid = ?", cId, cb);
}

/**
 * Update Project
 * @param {*} project 
 */
exports.updateProject = function(project, cb) {
    db.run("update tb_project set name = ?, cid = ?, orders = ? where id = ?", 
        [project.name, project.cId, project.orders, project.id], cb);
}